package net.xdclass.annotion;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author zhaowei
 * @Description TODO
 * @Remark 有问题直接联系我
 * @Version 1.0
 **/
public class AnnoTest {

    public static void main(String[] args) {

        TestCase testCase = new TestCase();
        Class<TestCase> testCaseClass = TestCase.class;
        //获取全部声明的方法
        Method[] declaredMethods = testCaseClass.getDeclaredMethods();
        //定义一个List，存储全部有Test注解，且要运行的方法
        List<Method> methodList = new ArrayList<>();

        //遍历方法，获取有注解且要运行
        for(Method method : declaredMethods){
            if(method.isAnnotationPresent(Test.class)){
                Test annotation = method.getAnnotation(Test.class);
                if(!annotation.disabled()){
                    //要运行的方法加到list
                    methodList.add(method);
                }
            }
        }
        //根据优先级排序,数字越小，越先执行
        methodList.sort(Comparator.comparingInt(obj -> obj.getAnnotation(Test.class).priority()));

        //遍历list，在invoke调用对应的方法
        for(Method method: methodList){
            try {
                method.invoke(testCase);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }

    }

}
